package com.carmanage.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.carmanage.dao.ICarshed;
import com.carmanage.domain.Car;
import com.carmanage.domain.Carschedule;
import com.carmanage.domain.Line;
import com.carmanage.util.PageController;

@Component
public class CarshedDaoImpl implements ICarshed {

	private SessionFactory sessionFactory;

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void saveCarshed(Carschedule carschedule) {
		Session session = sessionFactory.openSession();
		session.save(carschedule);
		session.flush();
		session.close();

	}

	public List<Carschedule> allCarschedules(PageController pc) {
		Session session = sessionFactory.openSession();
		Query query = session
				.createQuery("from Carschedule")
				.setFirstResult(pc.getPageStartRow())
				.setMaxResults(pc.getPageSize());
		List<Carschedule> carsched = query.list();
		session.close();
		return carsched;
	}

	public void deleteCarshedById(int schedule_id) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(
				"delete from Carschedule c where c.schedule_id=?");
		query.setInteger(0, schedule_id);
		query.executeUpdate();
		session.flush();
		session.close();
	}

	public int getTotalRows() {
		int totalRow = 0;
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(
				"select c from Carschedule c");
		totalRow = query.list().size();
		session.close();
		return totalRow;
	}

	public List<Line> allLineid() {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(
				"select l from Line l");
		List<Line> lines = query.list();
		session.close();
		return lines;
	}

	public String findPlate(String plate) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery(
				"select car.plate from Car car where car.plate=?");
		query.setString(0, plate);
		String plate1 = (String) query.uniqueResult();
		session.close();
		return plate1;

	}

	public List<Car> allPlate() {
		Session session =  sessionFactory.openSession();
		Query query = session.createQuery(
				"select c from Car c");
		List<Car> cars = query.list();
		session.close();
		return cars;
	}

	public List<Carschedule> findCarschedule(int runs_id, String plate,
			int line_id, String departure_time) {
		Session session =  sessionFactory.openSession();
		Query query = session
				.createQuery(
						"select c from Carschedule c where c.runs_id=? and c.plate=? and c.line_id=? and c.departure_time=?");
		query.setInteger(0, runs_id);
		query.setString(1, plate);
		query.setInteger(2, line_id);
		query.setString(3, departure_time);
		List<Carschedule> carschedules = query.list();
		session.close();
		return carschedules;

	}

	public int getTotalRows(int runs_id, String plate, int line_id,
			String departure_time) {
		int totalRow = 0;
		Session session =  sessionFactory.openSession();
		Query query = session
				.createQuery(
						"select c from Carschedule c where c.runs_id=? or c.plate=? or c.line_id=? or c.departure_time=?");
		query.setInteger(0, runs_id);
		query.setString(1, plate);
		query.setInteger(2, line_id);
		query.setString(3, departure_time);
		totalRow = query.list().size();
		session.close();
		return totalRow;

	}

	public Carschedule findCarshedById(int schedule_id) {
		Session session =  sessionFactory.openSession();
		Query query = session.createQuery(
				"select c from Carschedule c where c.schedule_id=?");
		query.setInteger(0, schedule_id);
		Carschedule carsched = (Carschedule)query.uniqueResult();
		session.close();
		return carsched;
	}

	public void updateCarshed(Carschedule carschedule) {
		Session session =  sessionFactory.openSession();
		session.saveOrUpdate(carschedule);
		session.flush();
		session.close();
	}

}
